package com.atguigu.gmall.mq.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.service.RabbitService;
import com.atguigu.gmall.mq.config.DeadLetterMqConfig;
import com.atguigu.gmall.mq.config.DelayMessageConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/mq")
public class SendMessageController {
//    @Autowired//生产者
//    private RabbitTemplate rabbitTemplate;//交换机和队列如何找到?
    @Autowired//应由服务类来处理
    private RabbitService rabbitService;

    //发消息 错误
//    @GetMapping("/sendMessage/{message}")
//    public Result sendMessage(@PathVariable String message) {
//        //会造成消息丢失  应答无回调方法  无法接受应答的消息
//        rabbitTemplate.convertAndSend("exchange123", "routingKey123", message);
//        //封装大量内容
//        return Result.ok();
//    }
    @GetMapping("/sendMessage/{message}")
    public Result sendMessage(@PathVariable String message) {
        //封装大量内容  交换机和队列在网页上创建 理应由java代码创建
        rabbitService.sendMessage("exchange123", "routingKey123", message);
        return Result.ok();
    }

    @GetMapping("/sendDeadLetterMessage/{message}/{ttl}")
    public Result sendDeadLetterMessage(@PathVariable String message,@PathVariable int ttl) {
        //封装大量内容  交换机和队列在网页上创建 理应由java代码创建
        rabbitService.sendDeadLetterMessage(DeadLetterMqConfig.exchange_dead,
                DeadLetterMqConfig.routing_dead_1, message,ttl);
        return Result.ok();
    }

    @GetMapping("/sendDelayedMessage/{message}/{delayTime}")
    public Result sendDelayedMessage(@PathVariable String message,@PathVariable int delayTime) {
        //封装大量内容  交换机和队列在网页上创建 理应由java代码创建
        rabbitService.sendDelayedMessage(DelayMessageConfig.exchange_delay,
                DelayMessageConfig.routing_delay,message,delayTime);
        //小bug 由于延迟消息开始在交换机中停留一段时间,队列一开始肯定收不到,会触发队列应答失败
        return Result.ok();
    }
}
